package feeinterface.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ulwx.tool.DbUtils;
import com.ulwx.tool.StringUtils;
import feeinterface.modeldb.BaseIPNetwork;

public class BaseIPNetworkDao {

	public static String getFormatedIP(String ip){
		String[] ipStrs=ip.split("\\.");
		String retIP="";
		for(int i=0; i< ipStrs.length; i++){
			retIP = retIP +
				StringUtils.
					paddingToFixedString(ipStrs[i], '0', 3, true);
		}
		return retIP;
	}
	public static BaseIPNetwork getByIP(String ip) throws Exception{
		
		String fip=getFormatedIP(ip);
		String sql="select * from BaseIPNetwork where StartIP<=? and " +
				"EndIP>=?";
//		DataBase db = DataBaseFactory.getDataBase();
		Map<Integer,Object> map=new HashMap<Integer,Object>();
		map.put(1, fip);
		map.put(2, fip);
		List<BaseIPNetwork> list=DbUtils.doQueryClass(null,BaseIPNetwork.class, sql, map);
		if(list.size()>=1) return list.get(0);
		
		return null;
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub

		System.out.println(getByIP("192.16.6.7"));
	}

}
